﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ServiceModel;

using SimpleBankClient.BankServiceReference;

namespace SimpleBankClient
{
    public partial class Form1 : Form
    {
        private BankContractClient proxy = null;
        private string AccountId = string.Empty;

        public Form1()
        {
            InitializeComponent();

            proxy = new BankContractClient();
            AccountId = ConfigurationManager.AppSettings.Get("TestAccount");
        }

        private void Deposit_Click(object sender, EventArgs e)
        {
            try
            {
                proxy.Deposit(AccountId, Decimal.Parse(DepositAmount.Text));
                //MessageBox.Show("OK");
                DepositAmount.Clear();
                Balance_Click(sender, e);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Deposit exception", MessageBoxButtons.OK);
            }
        }   

        private void Withdraw_Click(object sender, EventArgs e)
        {
            try
            {
                proxy.Withdraw(AccountId, Decimal.Parse(WithdrawAmount.Text));
                Balance_Click(sender, e);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void Balance_Click(object sender, EventArgs e)
        {
            try
            {
                BalanceBox.Text = proxy.GetBalance(AccountId).ToString();
                textBoxPending.Text = proxy.GetPendingAmount(AccountId).ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Balance exception", MessageBoxButtons.OK);
            }
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            proxy.SetCanBePending(checkBoxPending.Checked);
        }

        private void buttonReset_Click(object sender, EventArgs e)
        {
            proxy.Reset(AccountId);
            Balance_Click(sender, e);
        }

        private void DepositAmount_TextChanged(object sender, EventArgs e)
        {
            ButtonDeposit.Enabled = DepositAmount.Text.Trim().Length > 0;
        }

        private void WithdrawAmount_TextChanged(object sender, EventArgs e)
        {
            ButtonWithdraw.Enabled = WithdrawAmount.Text.Trim().Length > 0;
        }

        private void DepositAmount_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Return)
            {
                Deposit_Click(sender, e);
            }
            else if (e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9 
                || e.KeyCode == Keys.Delete || e.KeyCode == Keys.Back )
            {
                e.SuppressKeyPress = false;
            }
            else
            {
                e.SuppressKeyPress = true;
            }
        }

        private void WithdrawAmount_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Return)
            {
                Withdraw_Click(sender, e);
            }
            else if (e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9
                || e.KeyCode == Keys.Delete || e.KeyCode == Keys.Back)
            {
                e.SuppressKeyPress = false;
            }
            else
            {
                e.SuppressKeyPress = true;
            }
        }
    }
}
